
<!-- 项目类别 -->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['saas:class:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="classList">
      <el-table-column label="序号" type="index" width="55" />
      <el-table-column label="项目类别名称" align="center" prop="dictLabel" />
      <el-table-column label="服务归属" align="center">
        <template slot-scope="{ row }">
          <dict-tag
            :options="dict.type.service_items"
            :value="row.serviceItems"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="0"
            active-text="启用"
            :inactive-value="1"
            inactive-text="禁用"
            :disabled="upStatusDisabled"
            @change="changeStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-hasPermi="['saas:class:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            v-hasPermi="['saas:class:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        label-width="110px"
        :show-message="false"
      >
        <el-form-item label="项目类别名称" required prop="dictLabel">
          <el-input
            v-model="form.dictLabel"
            placeholder="请输入项目类别名称,限制30字之内"
            :maxlength="30"
          />
        </el-form-item>
        <el-form-item label="服务归属" required prop="serviceItems">
          <el-select
            v-model="form.serviceItems"
            placeholder="请选择服务归属"
            clearable
          >
            <el-option
              v-for="dict in dict.type.service_items"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDataList,
  addData,
  upData,
  deleteData,
  upStatus,
} from "@/api/system/class.js";
import { hasPermi } from '@/plugins/auth'

export default {
  name: "Class",
  dicts: ["service_items"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 总条数
      total: 0,

      classList: [],
    };
  },
  computed: {
    upStatusDisabled: function (){
      return !hasPermi('saas:class:upStatus')
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getDataList(this.queryParams).then((res) => {
        this.classList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        dictLabel: "",
        serviceItems: "",
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目类别";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改项目类别";
      this.form = {
        dictLabel: row.dictLabel,
        dictCode: row.dictCode,
        serviceItems: row.serviceItems,
      };
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("是否确认删除该类别？", "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteData({dictCode: row.dictCode}).then(res=>{
            this.$message.success("删除成功");
            this.getList();
        })
      });
    },
     // 修改状态
    changeStatus(row) {
      let params = {
        dictCode: row.dictCode,
        status: row.status
      };
      upStatus(params).then(() => {
        this.$message.success("操作成功");
        this.getList();
      });
    },
    // 添加| 修改 单位
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.dictCode) {
            upData(this.form).then(()=>{
                this.$message.success("修改成功");
                this.cancel();
                this.getList();
            })
          } else {
            addData(this.form).then(() => {
                this.$message.success("新增成功");
                this.cancel();
                this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
